import { getDeliveryInfo, getDriverList, getChuFillList, getFillList } from '@/gql/rehydrator';
import { toPublish } from '@mqtt';
import { defineStore } from 'pinia';
import { getDriverID } from '@/stores/driverID';
import { showLoading } from '@/utils/prompt';
export const fillingStation = defineStore('rehydrator', {
	state: () => ({
		id: uni.getStorageSync('delivery_id') ? uni.getStorageSync('delivery_id') : 0,
		name: '',
		price: '',
		address: '',
		money: '',
		getDriverList: [],
		getChuFillList: [],
		getFillList: [],
	}),
	getters: {},
	actions: {
		// 加注站信息
		getDeliveryIn(handler = () => { }) {
			const delivery_id = this.id
			showLoading('加载中');
			const payload = {
				query: getDeliveryInfo,
				variables: { delivery_id, driver_id: getDriverID() },
			};
			toPublish(
				'ql/delivery/getDeliveryInfo',
				payload,
				(obj : any) => {
					uni.hideLoading()
					const userInfo = obj.data;
					const { getDeliveryInfo } = userInfo;
					this.id = getDeliveryInfo.id
					this.name = getDeliveryInfo.name
					this.price = getDeliveryInfo.price
					this.address = getDeliveryInfo.address
					this.money = getDeliveryInfo.money
					uni.setStorageSync('delivery_id', getDeliveryInfo.id)
					handler();
				},
			);
		},
		//补液员列表
		filler() {
			showLoading('加载中');
			const payload = {
				query: getDriverList,
				variables: { delivery_id: uni.getStorageSync('delivery_id') },
			};
			toPublish(
				'ql/delivery/getDriverList',
				payload,
				(obj : any) => {
					uni.hideLoading()
					const { getDriverList } = obj.data;
					this.getDriverList = getDriverList
				},
			);
		},
		//受液机列表
		Receiver() {
			showLoading('加载中');
			const payload = {
				query: getFillList,
				variables: { delivery_id: uni.getStorageSync('delivery_id') },
			};
			toPublish('ql/delivery/getFillList', payload,
				(obj : any) => {
					uni.hideLoading()
					const { getFillList } = obj.data;
					this.getFillList = getFillList
				},
			);
		},
		//出液机列表
		machine() {
			showLoading('加载中');
			const payload = {
				query: getChuFillList,
				variables: { delivery_id: uni.getStorageSync('delivery_id') },
			};
			toPublish('ql/delivery/getChuFillList', payload,
				(obj : any) => {
					uni.hideLoading()
					const { getChuFillList } = obj.data;
					this.getChuFillList = getChuFillList
				},
			);
		}
	},
});